The instance of `obj` closest to (x, y) by straight-line distance, or nothing if none are alive. If `obj` is a parent, its children count as `obj`.
Works differently on Game Boy - On the Game Boy this call looks at every object of that kind one by one and is slow when there are many (asked of a parent object, every child kind too): use it a few times a frame at most. Distances count to 1/16 of a pixel.
Works differently on Game Boy Color - On the Game Boy Color this call looks at every object of that kind one by one and is slow when there are many (asked of a parent object, every child kind too): use it a few times a frame at most. Distances count to 1/16 of a pixel.
On other consoles this differs: see Side by side and How the consoles differ.
instance_nearest(x, y, obj) -> ref<object>| Name | Type |
|---|---|
x | real |
y | real |
obj | object |
ref<object>
Scans every live instance of an object and returns whichever one is closest to a point, by straight-line distance - or nothing if none are alive to find. This is a plain linear scan, not a spatial index, which is perfectly fine at this engine's scale (object pools are tens of instances, never thousands) but worth knowing if you are ever tempted to call it every frame from many instances at once.
x and y are a place in the room, counted from the room's top-left corner. That is the same way every instance's own x and y are counted, which is what lets the distances be compared fairly.
If obj is a parent, the nearest one can be any of its children. With obj_enemy as the parent of obj_slime and obj_bat, instance_nearest(x, y, obj_enemy) finds the closest enemy of any kind. The reference it gives back is typed as obj_enemy, so you can use the variables and events obj_enemy has through it.
A common shape: a turret re-picking its target from the nearest walker, the way the language overview's own first example does.
target = instance_nearest(x, y, obj_doc_walker);
if (target != nothing) {
with (instance_create(x, y, obj_doc_bolt)) {
direction = dir_to(x, y, other.target.x, other.target.y);
speed = 2;
}
}Shown here in Create - which is also where target has to be declared in the first place (hp = 10; in Create declares hp, this line declares target the same way - see Variables, types and scope) - so the turret already has a target and has already fired once before its own first Step even runs. Call the same lines again from Step or an Alarm (as target = instance_nearest(x, y, obj_doc_walker); there simply re-aims a target Create already declared) to keep re-aiming and firing every frame instead of just once. other.target reads the turret's own target from inside the with block, where self has switched to the new bolt. (A real turret would pace these shots with a timer rather than firing every single frame - see the language overview's own version of this example, which does exactly that with an Alarm event.)
A plain linear scan of every live instance of `obj` and of each of its descendants - each has its own pool, visited the parent first - fine at this engine’s scale (object pools are tens of instances, never thousands), not a spatial index. It still returns an instance you destroyed earlier in this same frame, so "nearest, destroy, nearest again" hands you the same one forever - see instance_destroy for the sweep that works instead.